<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;

/**
 * 内容输出类
 *
 * @author yuanzhang.yin
 * 
 */
header("content-type:text/html;charset=utf8");
class IndexController extends Controller{
     /**
    * 创建表
    * 通过传递过来的参数进行创建数据表
    * @author yuanzhang.yin <[email address]>
    * @version [1.0] [description]
    * @param content_output_id 内容输出平台的ID
    * @return array数组
    */
    public function actionCreatetable(){
        $table_name = Yii::$app->request->get('table_name');
        $select_sql = "SELECT table_name FROM content_output";
        $table_name_data = Yii::$app->db->createCommand($select_sql)->queryAll();
        $status = true;
        foreach ($table_name_data as $key => $value) {
            if ($value['table_name'] == $table_name) {
                $status = false;
            }
        }
        // var_dump($status);die;
        if (!$table_name) {
            echo "表名为空或者表名已存在，请重试";
            die;
        }
        $sql = "CREATE TABLE `".$table_name."` (
          `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
          `movie_link` varchar(50) NOT NULL COMMENT '电影链接',
          `clarity_type_name` varchar(20) NOT NULL COMMENT '清晰度',
          `movie_name` varchar(30) NOT NULL COMMENT '电影名称',
          `movie_describe` varchar(255) NOT NULL COMMENT '电影简介',
          `poster_transverse` varchar(30) DEFAULT NULL COMMENT '横版海报',
          `poster_upright` varchar(30) DEFAULT NULL COMMENT '竖版海报',
          `copyright_type_name` varchar(20) NOT NULL COMMENT '版权类型',
          `copyright_term` int(11) DEFAULT NULL COMMENT '版权期限',
          `movie_director` varchar(20) NOT NULL COMMENT '电影导演',
          `movie_performer` varchar(50) NOT NULL COMMENT '电影演员',
          `movie_type` varchar(30) NOT NULL COMMENT '电影类型',
          `movie_region` varchar(10) NOT NULL COMMENT '电影所属地区',
          `movie_year` varchar(15) NOT NULL COMMENT '电影所属年份',
          `movie_hour_length` int(4) NOT NULL COMMENT '电影时长',
          `format_type_name` varchar(20) NOT NULL COMMENT '格式类型名称',
          `movie_price` double(4,2) NOT NULL COMMENT '电影价格',
          `vip_within` tinyint(2) NOT NULL COMMENT '是否是VIP权限之类  1：是  0：不是',
          `content_source_name` varchar(20) NOT NULL COMMENT '内容源名称',
          `time` int(11) NOT NULL COMMENT '存放时间',
          PRIMARY KEY (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
        $res = Yii::$app->db->createCommand($sql)->execute();
        echo $res;
    }

    public function actionIndex(){
        /**
         * array_walk 和 foreach, for 的效率的比较。
         * 我们要测试的是foreach, for, 和 array_walk的效率的问题。 
         */
        // echo 1;die;
        //产生一个10000的一个数组。
        $max = 10000;
        $test_arr = range(0, $max);
        $temp = 0;
        //我们分别用三种方法测试求这些数加上1的值的时间。

        // for 的方法  
        $t1 = microtime(true);
        for ($i = 0; $i < $max; $i++) {
            $temp = $temp + 1;
        }
        $t2 = microtime(true);
        $t = $t2 - $t1;
        echo "就使用for, 没有对数组操作 花费: {$t}"."<br/>";

        $t1 = microtime(true);
        for ($i = 0; $i < $max; $i++) {
            $test_arr[$i] = $test_arr[$i] + 1;
        }
        $t2 = microtime(true);
        $t = $t2 - $t1;
        echo "使用for 并且直接对数组进行了操作 花费: {$t}"."<br/>";

        $t1 = microtime(true);
        for ($i = 0; $i < $max; $i++) {
            $this->addOne($test_arr[$i]);
        }
        $t2 = microtime(true);
        $t = $t2 - $t1;
        echo "使用for 调用函数对数组操作 花费 : {$t}"."<br/>";

        $t1 = microtime(true);
        foreach ($test_arr as $k => &$v) {
            $temp = $temp + 1;
        }
        $t2 = microtime(true);
        $t = $t2 - $t1;
        echo "使用 foreach 没有对数组操作 花费 : {$t}"."<br/>";

        $t1 = microtime(true);
        foreach ($test_arr as $k => &$v) {
            $v = $v + 1;
        }
        $t2 = microtime(true);
        $t = $t2 - $t1;
        echo "使用 foreach 直接对数组操作 : {$t}"."<br/>";

        $t1 = microtime(true);
        foreach ($test_arr as $k => &$v) {
            $this->addOne($v);
        }
        $t2 = microtime(true);
        $t = $t2 - $t1;
        echo "使用 foreach 调用函数对数组操作 : {$t}"."<br/>";

        $t1 = microtime(true);
        array_walk($test_arr, function($item){
            $item = $item + 1;
        });
        $t2 = microtime(true);
        $t = $t2 - $t1;
        echo "使用 array_walk 调用函数对数组操作 : {$t}";

        // $sql = "SELECT movie_link,movie_name,movie_describe,poster_transverse,poster_upright,copyright_term,movie_director,movie_performer,movie_type,movie_region,movie_year,movie_hour_length,movie_price,vip_within,clarity_type_name,copyright_type_name,format_type_name,content_source_name FROM movie_details INNER JOIN clarity_type ON movie_details.clarity_type_id = clarity_type.clarity_type_id INNER JOIN copyright_type ON movie_details.copyright_type_id = copyright_type.copyright_type_id INNER JOIN format_type ON movie_details.format_type_id = format_type.format_type_id INNER JOIN content_source ON movie_details.content_source_id = content_source.content_source_id";
        // $res  = Yii::$app->db->createCommand($sql)->queryAll();
        // echo "<pre>";
        // var_dump($res);
    }

    public function addOne($item){
       $item = $item + 1;
    }

   /**
    * 数据请求
    * 前端通过传递参数来获取对应的电影信息
    * @author yuanzhang.yin <[email address]>
    * @version [1.0] [description]
    * @param type = 
    * @return json字符串
    */
    public function actionDatarequest(){
        $type = Yii::$app->request->get('type');
        $result = array();
        if ($type) {
            $content_source_sql = "SELECT content_output_id FROM content_output";
            $data = Yii::$app->db->createCommand($content_source_sql)->queryAll();
            $arr = array();
            foreach ($data as $key => $value) {
                $arr[] = $value['content_output_id'];
            }
            if (in_array($type,$arr)) {
                $rule = $this->get_rule($type);
                $movie_data = $this->get_movie_data();
                echo "<pre>";
                // echo $movie_data;die;
                var_dump($movie_data);die;
            }else{
                $result['code'] = '10002';
                $result['data'] = '参数有误';
            }
        }else{
            $result['code'] = '10001';
            $result['data'] = '未携带有效参数'; 
        }
        echo "<pre>";
        var_dump($result);
    }

    /**
    * 获取规则
    * 通过参数获取到对应输出平台的黑名单规则
    * @author yuanzhang.yin <[email address]>
    * @version [1.0] [description]
    * @param content_output_id 内容输出平台的ID
    * @return array数组
    */
    public function get_rule($content_output_id){
        $sql = "SELECT copyright_type_id,format_type_id,content_source_id FROM content_output_rule WHERE content_output_id = ".$content_output_id;
        $data = Yii::$app->db->createCommand($sql)->queryAll();
        $rule = array();
        foreach ($data as $key => $value) {
            $rule[$value['content_source_id']][] = $value;
        }
        return $rule;
    }

    /**
    * 获取电影信息
    * 通过规则来进行电影信息的筛选
    * @author yuanzhang.yin <[email address]>
    * @version [1.0] [description]
    * @param rule 此内容输出平台的黑名单规则
    * @return array数组
    */
    public function get_movie_data($rule = ''){
        $sql = "SELECT movie_link,movie_name,movie_describe,poster_transverse,poster_upright,copyright_term,movie_director,movie_performer,movie_type,movie_region,movie_year,movie_hour_length,movie_price,vip_within,clarity_type_name,copyright_type_name,format_type_name,content_source_name FROM movie_details INNER JOIN clarity_type ON movie_details.clarity_type_id = clarity_type.clarity_type_id INNER JOIN copyright_type ON movie_details.copyright_type_id = copyright_type.copyright_type_id INNER JOIN format_type ON movie_details.format_type_id = format_type.format_type_id INNER JOIN content_source ON movie_details.content_source_id = content_source.content_source_id WHERE 1=1";
        if (empty($rule)) {
            $movie_data = Yii::$app->db->createCommand($sql)->queryAll();
        }else{
            $sql .= " && ";
            foreach ($rule as $key => $value) {
                $sql .= '(';
                foreach ($value as $k => $val) {
                    if ($val['copyright_type_id']) {
                        $sql .= "( content_source_id "." = ". $val['content_source_id'] ." && ". " copyright_type_id " ." != ". $val['copyright_type_id']." ) " .' && ' ;
                    }else{
                        $sql .= "( content_source_id "." = ". $val['content_source_id'] ." && ". " format_type_id " ." != ". $val['format_type_id']." ) " .' && ' ;
                    }
                }
                $sql = trim($sql,'&& ');
                $sql .= ') ||  ';
            }
            $sql = trim($sql,'||  ');
            return $sql;die;
            $movie_data = Yii::$app->db->createCommand($sql)->queryAll();
        }
        return $movie_data;
    }
}
